package br.com.dataphonebr.adapter;

import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.graphics.Typeface;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;
import br.com.dataphonebr.R;

public abstract class GenericExpandableListAdapter<T> extends BaseExpandableListAdapter {
	
	protected Context _context;
	protected ExpandableListAdapter listAdapter;
    protected List<Object> _listDataHeader;
    protected HashMap<String, List<Object>> _listDataChild;//TODO: Verificar tipos
    private ExpandableListView expListView;
    
    protected abstract void prepareList();
	
    public GenericExpandableListAdapter(Context context) {
    	this._context = context;
	}
    
    protected void createAdapter(ExpandableListView expListView) {
    	expListView.setAdapter(listAdapter);
    	this.expListView = expListView;
    }
    
    public ExpandableListView getExpListView() {
		return expListView;
	}
    
	@Override
	public Object getChild(int groupPosition, int childPosititon) {
		return this._listDataChild.get(this._listDataHeader.get(groupPosition)).get(childPosititon);
	}

	@Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }
	
	public abstract String prepareChildView(Object object, int groupPosition);
 
    @Override
    public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
    	
        final Object childObject = (Object) getChild(groupPosition, childPosition);
        String childText = prepareChildView(childObject, groupPosition);
 
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.exp_item_list, null);
        }
 
        TextView txtListChild = (TextView) convertView.findViewById(R.id.lblExpListItem);
        txtListChild.setText(Html.fromHtml(childText));
        
//		TextView popupBtn = (TextView)convertView.findViewById(R.id._showPopupExpItemBtn);
//		popupBtn.setTag(R.id.TAG_GROUP_POSITION, groupPosition);
//		popupBtn.setTag(R.id.TAG_CHILD_POSITION, childPosition);
        
        return convertView;
    }
    
    @Override
    public int getChildrenCount(int groupPosition) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition)).size();
    }
 
    @Override
    public Object getGroup(int groupPosition) {
        return this._listDataHeader.get(groupPosition);
    }
 
    @Override
    public int getGroupCount() {
        return this._listDataHeader.size();
    }
 
    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,View convertView, ViewGroup parent) {
        String headerTitle = (String) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.exp_list_group, null);
        }
 
        TextView lblListHeader = (TextView) convertView.findViewById(R.id.lblExpListHeader);
        lblListHeader.setTypeface(null, Typeface.BOLD);
        lblListHeader.setText(Html.fromHtml(headerTitle));
 
        return convertView;
    }
 
    @Override
    public boolean hasStableIds() {
        return false;
    }
 
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}
